<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Redis 哈希(Hash) | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Redis 哈希(Hash)</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis hash 是一个string类型的field和value的映射表，hash特别适合用于存储对象。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis 中每个 hash 可以存储 232 - 1 键值对（40多亿）。</p>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; HMSET runoobkey name <span class="string">"redis tutorial"</span> description <span class="string">"redis basic commands for caching"</span> likes 20 visitors 23000</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; HGETALL runoobkey</div><div class="line">1) <span class="string">"name"</span></div><div class="line">2) <span class="string">"redis tutorial"</span></div><div class="line">3) <span class="string">"description"</span></div><div class="line">4) <span class="string">"redis basic commands for caching"</span></div><div class="line">5) <span class="string">"likes"</span></div><div class="line">6) <span class="string">"20"</span></div><div class="line">7) <span class="string">"visitors"</span></div><div class="line">8) <span class="string">"23000"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在以上实例中，我们设置了 redis 的一些描述信息(name, description, likes, visitors) 到哈希表的 <strong>runoobkey</strong> 中。</p>
<h2 id="Redis-hash-命令"><a href="#Redis-hash-命令" class="headerlink" title="Redis hash 命令"></a>Redis hash 命令</h2><h3 id="1-Redis-Hdel-命令"><a href="#1-Redis-Hdel-命令" class="headerlink" title="1.Redis Hdel 命令"></a>1.Redis Hdel 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HDEL key field2 [field2]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>删除一个或多个哈希表字段</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段，不存在的字段将被忽略。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hdel 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HDEL KEY_NAME FIELD1.. FIELDN</div></pre></td></tr></table></figure>
<h4 id="可用版本"><a href="#可用版本" class="headerlink" title="可用版本"></a>可用版本</h4><h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被成功删除字段的数量，不包括被忽略的字段。</p>
<h4 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HDEL myhash field1</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HDEL myhash field2</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="2-Redis-Hexists-命令"><a href="#2-Redis-Hexists-命令" class="headerlink" title="2.Redis Hexists 命令"></a>2.Redis Hexists 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HEXISTS key field</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<figure class="highlight plain"><figcaption><span>key 中，指定的字段是否存在```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;Redis Hexists 命令用于查看哈希表的指定字段是否存在。</div><div class="line"></div><div class="line">#### 语法</div><div class="line">&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;redis Hexists 命令基本语法如下：</div><div class="line"></div><div class="line">```bash</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS KEY_NAME FIELD_NAME</div></pre></td></tr></table></figure></p>
<h4 id="可用版本-1"><a href="#可用版本-1" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-1"><a href="#返回值-1" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表含有给定字段，返回 1 。 如果哈希表不含有给定字段，或 key 不存在，返回 0 。</p>
<h4 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS myhash field1</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HEXISTS myhash field2</div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="3-Redis-Hget-命令"><a href="#3-Redis-Hget-命令" class="headerlink" title="3.Redis Hget 命令"></a>3.Redis Hget 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HGET key field</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取存储在哈希表中指定字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hget 命令用于返回哈希表中指定字段的值。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hget 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HGET KEY_NAME FIELD_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-2"><a href="#可用版本-2" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-2"><a href="#返回值-2" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回给定字段的值。如果给定的字段或 key 不存在时，返回 nil 。</p>
<h4 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 字段存在</span></div><div class="line"> </div><div class="line">redis&gt; HSET site redis redis.com</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis&gt; HGET site redis</div><div class="line"><span class="string">"redis.com"</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="comment"># 字段不存在</span></div><div class="line"> </div><div class="line">redis&gt; HGET site mysql</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<h3 id="4-Redis-Hgetall-命令"><a href="#4-Redis-Hgetall-命令" class="headerlink" title="4..Redis Hgetall 命令"></a>4..Redis Hgetall 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HGETALL key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取在哈希表中指定 key 的所有字段和值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hgetall 命令用于返回哈希表中，所有的字段和值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在返回值里，紧跟每个字段名(field name)之后是字段的值(value)，所以返回值的长度是哈希表大小的两倍。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hgetall 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HGETALL KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-3"><a href="#可用版本-3" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-3"><a href="#返回值-3" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以列表形式返回哈希表的字段及字段值。 若 key 不存在，返回空列表。</p>
<h4 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HGETALL myhash</div><div class="line">1) <span class="string">"field1"</span></div><div class="line">2) <span class="string">"Hello"</span></div><div class="line">3) <span class="string">"field2"</span></div><div class="line">4) <span class="string">"World"</span></div></pre></td></tr></table></figure>
<h3 id="5-Redis-Hincrby-命令"><a href="#5-Redis-Hincrby-命令" class="headerlink" title="5.Redis Hincrby 命令"></a>5.Redis Hincrby 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HINCRBY key field increment</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>为哈希表 key 中的指定字段的整数值加上增量 increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增量也可以为负数，相当于对指定字段进行减法操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表的 key 不存在，一个新的哈希表被创建并执行 HINCRBY 命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在，那么在执行命令前，字段的值被初始化为 0 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本操作的值被限制在 64 位(bit)有符号数字表示之内。</p>
<h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hincrby 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HINCRBY KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-4"><a href="#可用版本-4" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-4"><a href="#返回值-4" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 HINCRBY 命令之后，哈希表中字段的值。</p>
<h4 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">redis&gt; HSET myhash field 5</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; HINCRBY myhash field 1</div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line">redis&gt; HINCRBY myhash field -1</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">redis&gt; HINCRBY myhash field -10</div><div class="line">(<span class="built_in">integer</span>) -5</div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="6-Redis-Hincrbyfloat-命令"><a href="#6-Redis-Hincrbyfloat-命令" class="headerlink" title="6.Redis Hincrbyfloat 命令"></a>6.Redis Hincrbyfloat 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HINCRBYFLOAT key field increment</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>为哈希表 key 中的指定字段的浮点数值加上增量 increment</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在，那么在执行命令前，字段的值被初始化为 0 。</p>
<h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hincrbyfloat 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HINCRBYFLOAT KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-5"><a href="#可用版本-5" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.6.0</p>
<h4 id="返回值-5"><a href="#返回值-5" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行 Hincrbyfloat 命令之后，哈希表中字段的值。</p>
<h4 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">redis&gt; HSET mykey field 10.50</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis&gt; HINCRBYFLOAT mykey field 0.1</div><div class="line"><span class="string">"10.60000000000000001"</span></div><div class="line">redis&gt; HINCRBYFLOAT mykey field -5</div><div class="line"><span class="string">"5.59999999999999964"</span></div><div class="line">redis&gt; HSET mykey field 5.0e3</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis&gt; HINCRBYFLOAT mykey field 2.0e2</div><div class="line"><span class="string">"5200"</span></div><div class="line">redis&gt;</div></pre></td></tr></table></figure>
<h3 id="7-Redis-Hkeys-命令"><a href="#7-Redis-Hkeys-命令" class="headerlink" title="7.Redis Hkeys 命令"></a>7.Redis Hkeys 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HKEYS key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取所有哈希表中的字段</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hkeys 命令用于获取哈希表中的所有字段名。</p>
<h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hkeys 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HKEYS KEY_NAME FIELD_NAME INCR_BY_NUMBER</div></pre></td></tr></table></figure>
<h4 id="可用版本-6"><a href="#可用版本-6" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-6"><a href="#返回值-6" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;包含哈希表中所有字段的列表。 当 key 不存在时，返回一个空列表。</p>
<h4 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HKEYS myhash</div><div class="line">1) <span class="string">"field1"</span></div><div class="line">2) <span class="string">"field2"</span></div></pre></td></tr></table></figure>
<h3 id="8-Redis-Hlen-命令"><a href="#8-Redis-Hlen-命令" class="headerlink" title="8.Redis Hlen 命令"></a>8.Redis Hlen 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HLEN key</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取哈希表中字段的数量</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hlen 命令用于获取哈希表中字段的数量。</p>
<h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hlen 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HLEN KEY_NAME</div></pre></td></tr></table></figure>
<h4 id="可用版本-7"><a href="#可用版本-7" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-7"><a href="#返回值-7" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;哈希表中字段的数量。 当 key 不存在时，返回 0 。</p>
<h4 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HLEN myhash</div><div class="line">(<span class="built_in">integer</span>) 2</div></pre></td></tr></table></figure>
<h3 id="9-Redis-Hmget-命令"><a href="#9-Redis-Hmget-命令" class="headerlink" title="9.Redis Hmget 命令"></a>9.Redis Hmget 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HMGET key field1 [field2]</code><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取所有给定字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hmget 命令用于返回哈希表中，一个或多个给定字段的值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果指定的字段不存在于哈希表，那么返回一个 nil 值。</p>
<h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hmget 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMGET KEY_NAME FIELD1...FIELDN</div></pre></td></tr></table></figure>
<h4 id="可用版本-8"><a href="#可用版本-8" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-8"><a href="#返回值-8" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个包含多个给定字段关联值的表，表值的排列顺序和指定字段的请求顺序一样。</p>
<h4 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HMGET myhash field1 field2 nofield</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div><div class="line">3) (nil)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HMSET key field1 value1 [field2 value2 ]</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>同时将多个 field-value (域-值)对设置到哈希表 key 中</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此命令会覆盖哈希表中已存在的字段。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，会创建一个空哈希表，并执行 HMSET 操作。</p>
<h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hmset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMSET KEY_NAME FIELD1 VALUE1 ...FIELDN VALUEN</div></pre></td></tr></table></figure>
<h4 id="可用版本-9"><a href="#可用版本-9" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-9"><a href="#返回值-9" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果命令执行成功，返回 OK 。</p>
<h4 id="实例-10"><a href="#实例-10" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HMSET myhash field1 <span class="string">"Hello"</span> field2 <span class="string">"World"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"Hello"</span></div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field2</div><div class="line"><span class="string">"World"</span></div></pre></td></tr></table></figure>
<h3 id="11-Redis-Hset-命令"><a href="#11-Redis-Hset-命令" class="headerlink" title="11.Redis Hset 命令"></a>11.Redis Hset 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HSET key field value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>将哈希表 key 中的字段 field 的值设为 value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hset 命令用于为哈希表中的字段赋值 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，一个新的哈希表被创建并进行 HSET 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段已经存在于哈希表中，旧值将被覆盖。</p>
<h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hset 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-10"><a href="#可用版本-10" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-10"><a href="#返回值-10" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段是哈希表中的一个新建字段，并且值设置成功，返回 1 。 如果哈希表中域字段已经存在且旧值已被新值覆盖，返回 0 。</p>
<h4 id="实例-11"><a href="#实例-11" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">OK</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"foo"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSET website google <span class="string">"www.g.cn"</span> <span class="comment"># 设置一个新域</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt;HSET website google <span class="string">"www.google.com"</span> <span class="comment"># 覆盖一个旧域</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="12-Redis-Hsetnx-命令"><a href="#12-Redis-Hsetnx-命令" class="headerlink" title="12.Redis Hsetnx 命令"></a>12.Redis Hsetnx 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HSETNX key field value</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>只有在字段 field 不存在时，设置哈希表字段的值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果哈希表不存在，一个新的哈希表被创建并进行 HSET 操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果字段已经存在于哈希表中，操作无效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 key 不存在，一个新哈希表被创建并执行 HSETNX 命令。</p>
<h4 id="语法-10"><a href="#语法-10" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hsetnx 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSETNX KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-11"><a href="#可用版本-11" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-11"><a href="#返回值-11" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置成功，返回 1 。 如果给定字段已经存在且没有操作被执行，返回 0 。</p>
<h4 id="实例-12"><a href="#实例-12" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSETNX myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSETNX myhash field1 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">redis 127.0.0.1:6379&gt; HGET myhash field1</div><div class="line"><span class="string">"foo"</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSETNX nosql key-value-store redis</div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HSETNX nosql key-value-store redis <span class="comment"># 操作无效， key-value-store 已存在</span></div><div class="line">(<span class="built_in">integer</span>) 0</div></pre></td></tr></table></figure>
<h3 id="13-Redis-Hvals-命令"><a href="#13-Redis-Hvals-命令" class="headerlink" title="13.Redis Hvals 命令"></a>13.Redis Hvals 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>HVALS key</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>获取哈希表中所有值</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Redis Hvals 命令返回哈希表所有字段的值。</p>
<h4 id="语法-11"><a href="#语法-11" class="headerlink" title="语法"></a>语法</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redis Hvals 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HVALS KEY_NAME FIELD VALUE</div></pre></td></tr></table></figure>
<h4 id="可用版本-12"><a href="#可用版本-12" class="headerlink" title="可用版本"></a>可用版本</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;= 2.0.0</p>
<h4 id="返回值-12"><a href="#返回值-12" class="headerlink" title="返回值"></a>返回值</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个包含哈希表中所有值的表。 当 key 不存在时，返回一个空表。</p>
<h4 id="实例-13"><a href="#实例-13" class="headerlink" title="实例"></a>实例</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field1 <span class="string">"foo"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HSET myhash field2 <span class="string">"bar"</span></div><div class="line">(<span class="built_in">integer</span>) 1</div><div class="line">redis 127.0.0.1:6379&gt; HVALS myhash</div><div class="line">1) <span class="string">"foo"</span></div><div class="line">2) <span class="string">"bar"</span></div><div class="line"> </div><div class="line"><span class="comment"># 空哈希表/不存在的key</span></div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; EXISTS not_exists</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line"> </div><div class="line">redis 127.0.0.1:6379&gt; HVALS not_exists</div><div class="line">(empty list or <span class="built_in">set</span>)</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/19. Redis 哈希(Hash)/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
